package com.bhs.service.Product.impl;

import com.bhs.mappers.Product.ProductMapper;
import com.bhs.mappers.menu.LeftMenuMapper;
import com.bhs.pojo.Item;
import com.bhs.pojo.MainMenu;
import com.bhs.redis.Redis;
import com.bhs.service.Product.ProductService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private LeftMenuMapper leftMenuMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public Item getDetails(Integer id) {
        return productMapper.getDetails(id);
    }

    @Override
    public List<MainMenu> getGoodsList() {
        // 使用Redis
        if (Redis.redisJedis().exists("mainMenuLists")) {
            String mainMenuLists = Redis.redisJedis().get("mainMenuLists");
            try {
                List<MainMenu> list = new ObjectMapper().readValue(mainMenuLists, List.class);
                return list;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

        }
        //获取MainMenu表数据并用List保存，这个里面没children
        List<MainMenu> mainMenus = leftMenuMapper.getMainMenu();
        //这个是新建的传回前端的，有children
        List<MainMenu> mainMenuLists = new ArrayList<>();
        //Item表k是表中mainMenuId，v是表数据
        Map<Integer, List<Item>> maps = getGoodsMap();
        System.out.println();
        //把MainMenu表中的数据便利出来，这个里面的children没有
        for (MainMenu items : mainMenus) {
            //获取MainMenu表id
            Integer mainMenuId = items.getId();
            //根据mainMenuId获取Item表并存入List中
            List<Item> items1 = maps.get(mainMenuId);
            //如果为空则说明mainMenuId对应的类型没有则直接结束
            if (items1 == null) {
                continue;
            }
            //把上面获取的Item数据写入MainMenu的children里
            items.setChildren(items1);
            mainMenuLists.add(items);
        }
        try {
            String value = new ObjectMapper().writeValueAsString(mainMenuLists);
            Redis.redisJedis().set("mainMenuLists", value);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return mainMenuLists;
    }

    @Override
    public List<Item> getGoodsList1() {
        return productMapper.getGoodsList1();
    }

    @Override
    public List<Item> getGoodsList2() {
        return productMapper.getGoodsList2();
    }

    @Override
    public List<Item> getGoodsList3() {
        return productMapper.getGoodsList3();
    }

    @Override
    public List<Item> getGoodsList4() {
        return productMapper.getGoodsList4();
    }

    @Override
    public List<Item> getGoodsList5() {
        return productMapper.getGoodsList5();
    }

    @Override
    public List<Item> getGoodsList6() {
        return productMapper.getGoodsList6();
    }

    public Map<Integer, List<Item>> getGoodsMap() {
        HashMap<Integer, List<Item>> maps = new HashMap<>();
        //获取Item表
        List<Item> itemList = leftMenuMapper.getMainItem();
        //把Item表遍历出来
        for (Item item : itemList) {
            //获取item表的mainMenuId
            Integer key = item.getMainMenuId();
            //把Item表和Item表中的mainMenuId作为一对<k,v>传入maps中
            if (maps.containsKey(key)) {
                //如果K存在，则只改变V，增加到list中
                List<Item> list = maps.get(key);
                list.add(item);
                continue;
            }
            List<Item> list = new ArrayList<>();
            list.add(item);
            maps.put(key, list);
        }
        return maps;
    }

}
